package com.dyz.wsw.passport.controller;

import com.dyz.wsw.common.utils.security.KeyGenerator;
import com.dyz.wsw.common.web.JsonResult;
import com.dyz.wsw.security.config.WswSecurityJWTProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.security.KeyPair;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @author dyz
 * @date 2025/2/7 17:53
 */
@RestController
@RequestMapping("/key")
@Slf4j
public class KeyController {

    private final WswSecurityJWTProperties properties;

    public KeyController(WswSecurityJWTProperties properties){
        log.info("创建控制器对象: KeyController");
        this.properties = properties;
    }

    @GetMapping("/pub")
    public Mono<JsonResult> getPubKey(){
        return Mono.just(JsonResult.ok(properties.getPublicKey()));
    }

    @GetMapping("/generate/keys")
    public Mono<JsonResult> generateKeys(){
        KeyPair keyPair = KeyGenerator.generateKeyPair();
        String privateKey = KeyGenerator.getPrivateKeyAsString(keyPair.getPrivate());
        String publicKey = KeyGenerator.getPublicKeyAsString(keyPair.getPublic());
        HashMap<String, String> keys = new LinkedHashMap<>();
        keys.put("publicKey", publicKey);
        keys.put("privateKey", privateKey);
        return Mono.just(JsonResult.ok(keys));
    }

}
